<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['createBy']" placeholder="请输入创建人"></a-input>
        </a-form-item>
        <a-form-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择创建日期" v-decorator="['createTime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="制令单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['commandbillCode']" placeholder="请输入制令单号"></a-input>
        </a-form-item>
        <a-form-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['workbillCode']" placeholder="请输入工单号"></a-input> -->
          <j-popup
                      v-decorator="['workbillCode']"
                      :trigger-change="true"
                      org-fields="id,workbill_code,produce_grade,mechanism_code,mechanism_name,gague,plant_num,client"
                      dest-fields="workbillId,workbillCode,produceGrade,mechanismCode,mechanismName,gague,plantNum,clientName"
                      code="workbill_search"
                      @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="生产阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['produceGrade']" :trigger-change="true" dictCode="produce_grade" placeholder="请选择生产阶别"/>
        </a-form-item>
        <a-form-item label="线别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['lineType']" placeholder="请输入线别"></a-input> -->
                    <j-popup
                      v-decorator="['lineType']"
                      :trigger-change="true"
                      org-fields="id,productline_name"
                      dest-fields="lineId,lineType"
                      code="productline_search"
                      @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="加工面别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['processFace']" :trigger-change="true" dictCode="process_face" placeholder="请选择加工面别"/>
        </a-form-item>
        <a-form-item label="PCB料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['pcbCode']" placeholder="请输入PCB料号"></a-input>
        </a-form-item>
        <a-form-item label="机种料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['mechanismCode']" placeholder="请输入机种料号"></a-input>
        </a-form-item>
        <a-form-item label="机种名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['mechanismName']" placeholder="请输入机种名称"></a-input>
        </a-form-item>
        <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['gague']" placeholder="请输入规格"></a-input>
        </a-form-item>
        <a-form-item label="工艺" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['progress']" placeholder="请输入工艺"></a-input>
        </a-form-item>
        <a-form-item label="起始工序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['startProgress']" placeholder="请输入起始工序"></a-input>
        </a-form-item>
        <a-form-item label="结束工序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['endProgress']" placeholder="请输入结束工序"></a-input>
        </a-form-item>
        <a-form-item label="预计开工时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择预计开工时间" v-decorator="['expectBegintime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="预计完工时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择预计完工时间" v-decorator="['expectFinishtime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="下达时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择下达时间" v-decorator="['arriveTime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="计划数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['plantNum']" placeholder="请输入计划数量"></a-input>
        </a-form-item>
        <a-form-item label="联板数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['linkBoardnum']" placeholder="请输入联板数"></a-input>
        </a-form-item>
        <a-form-item label="条码拼板数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['barcodeBoardnum']" placeholder="请输入条码拼板数"></a-input>
        </a-form-item>
        <a-form-item label="产品条码规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['productCoderule']" placeholder="请输入产品条码规则"></a-input>
        </a-form-item>
        <a-form-item label="小板条码规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['smallCoderule']" placeholder="请输入小板条码规则"></a-input>
        </a-form-item>
        <a-form-item label="中转条码规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['transitCoderule']" placeholder="请输入中转条码规则"></a-input>
        </a-form-item>
        <a-form-item label="不良报废" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['badWriteoff']" :trigger-change="true" dictCode="yn" placeholder="请选择不良报废"/>
        </a-form-item>
        <a-form-item label="管控类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['controlType']" placeholder="请输入管控类型"></a-input>
        </a-form-item>
        <a-form-item label="维修上限次数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['repairLimit']" placeholder="请输入维修上限次数"></a-input>
        </a-form-item>
        <!-- <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientName']" placeholder="请输入客户名称"></a-input>
        </a-form-item> -->
        <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!--<a-input v-decorator="['client']" placeholder="请输入客户"></a-input>-->
          <j-popup
            v-decorator="['clientName',validatorRules.clientName]"
            :trigger-change="true"
            org-fields="client_name"
            dest-fields="clientName"
            code="client_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['notes']" rows="4" placeholder="请输入备注"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesCommandbillInfoModal",
    components: { 
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          clientName: {
            rules: [{
              required: true,
              message: '请选择客户!'
            }, ]
          },
        },
        url: {
          add: "/produce/mesCommandbillInfo/add",
          edit: "/produce/mesCommandbillInfo/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','commandbillCode','workbillCode','produceGrade','lineType','processFace','pcbCode','mechanismCode','mechanismName','gague','progress','startProgress','endProgress','expectBegintime','expectFinishtime','arriveTime','plantNum','linkBoardnum','barcodeBoardnum','productCoderule','smallCoderule','transitCoderule','badWriteoff','controlType','repairLimit','clientName','notes'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','commandbillCode','workbillCode','produceGrade','lineType','processFace','pcbCode','mechanismCode','mechanismName','gague','progress','startProgress','endProgress','expectBegintime','expectFinishtime','arriveTime','plantNum','linkBoardnum','barcodeBoardnum','productCoderule','smallCoderule','transitCoderule','badWriteoff','controlType','repairLimit','clientName','notes'))
      },

      
    }
  }
</script>